<template>
	<view>
		<!-- 分配工程经理 -->
		<u-picker
			ref="picker"
			:title="type == '7' ? '工程经理' : '工程监理'"
			:show="gcjlShow2"
			:columns="columnsList"
			keyName="name"
			@close="choosefpgcjFunc2"
			@cancel="choosefpgcjFunc2"
			@confirm="gcjlConfirm"
			closeOnClickOverlay
		></u-picker>
		<u-popup :show="gcjlShow" @close="close('add_manager')" mode="bottom" closeable round="10">
			<view class="fp-box-title">分配施工工程经理</view>
			<view class="fp-box">
				<u--form labelPosition="top" :model="fpgcjlRuleForm" labelWidth="auto">
					<u-form-item required label="工程经理" borderBottom @click="choosefpgcjFunc(7)">
						<u--input v-model="fpgcjlRuleForm.engineering_manager_name" readonly border="none" placeholder="选择工程经理" suffixIcon="arrow-right"></u--input>
					</u-form-item>
					<u-form-item required label="工程监理" borderBottom @click="choosefpgcjFunc(8)">
						<u--input v-model="fpgcjlRuleForm.engineering_supervision_name" readonly border="none" placeholder="选择工程监理" suffixIcon="arrow-right"></u--input>
					</u-form-item>
					<u-form-item label="备注">
						<u--textarea v-model="fpgcjlRuleForm.remark"></u--textarea>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="submitFunc"></u-button>
			</view>
		</u-popup>
		<!-- 拒绝接单 -->
		<u-popup :show="gcjljjjdVisible" @close="close('refuse_orders')" mode="bottom" closeable round="10">
			<view class="fp-box">
				<view class="fp-box-title">拒绝接单</view>
				<u--form labelPosition="top" :model="gcjljjjdRuleForm" labelWidth="auto">
					<u-form-item required label="备注">
						<u--textarea v-model="gcjljjjdRuleForm.remark"></u--textarea>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="jjSubmitFunc"></u-button>
			</view>
		</u-popup>
		<!-- 更换工程经理 -->
		<changeManCom @closeFunc="close('edit_manager')" :show="changShow" :type="7" :projectId="oper_item.id" @changeManSuccessFunc="changeManSuccessFunc"></changeManCom>
		<!-- 质量评分 -->
		<u-popup :show="zlpfShow" @close="close('projects_projectQualityRating')" mode="bottom" closeable round="10">
			<view class="fp-box-title">质量评分</view>
			<view class="fp-box">
				<u--form labelPosition="top" :model="zlpfRuleForm" labelWidth="auto">
					<u-form-item required label="评分">
						<view class="u-rate">
							<!-- <u-rate gutter="6" activeColor="rgb(247, 186, 42)" size="26" :count="5" v-model="zlpfRuleForm.project_quality_rating"></u-rate> -->
							<u--input @input="inputFunc" type="digit" placeholder="1~100分" border="surround" v-model="zlpfRuleForm.project_quality_rating"></u--input>
						</view>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="zlpfSubmitFunc"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	name: 'constructionOperate',
	props: {
		value: {
			type: String,
			default: ''
		},
		oper_item: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {
			changShow: false,
			engineeringSupervisionList: [], //工程经理列表
			engineeringSupervisionIdList: [], //监理列表
			fpgcjlRuleForm: {
				engineering_manager_id: '',
				engineering_manager_name: '',
				engineering_supervision_id: '',
				engineering_supervision_name: '',
				remark: ''
			},
			gcjlShow: false, //分配工程经理弹窗
			gcjlShow2: false, //工程经理弹窗
			type: '', // 7 / 8
			columnsList: [],
			// 拒绝接单
			gcjljjjdVisible: false,
			gcjljjjdRuleForm: {
				remark: ''
			},
			zlpfShow: false, //质量评分弹窗
			zlpfRuleForm: {
				project_quality_rating: ''
			} //质量评分数据
		};
	},
	watch: {
		value(val) {
			let that = this;
			switch (val) {
				// 分配施工工程经理
				case 'add_manager':
					this.getEngineering_directorList(7);
					this.getEngineering_directorList(8);
					this.gcjlShow = true;
					break;
				// 拒绝接单
				case 'refuse_orders':
					this.gcjljjjdVisible = true;
					break;
				// 更换工程经理
				case 'edit_manager':
					this.changShow = true;
					break;
				// 开工
				case 'construction_submit':
					uni.navigateTo({
						url: '/pagesC/engineeringManagement/qrkssgCom/qrkssgCom?id=' + this.oper_item.id
					});
					this.close('construction_submit');
					break;
				// 质量评分
				case 'projects_projectQualityRating':
					this.zlpfShow = true;
					break;
				default:
			}
		}
	},

	methods: {
		inputFunc(e) {
			console.log(this.$inputFunc2(e))
			this.$nextTick(() => {
				this.zlpfRuleForm.project_quality_rating = this.$inputFunc2(e);
			});
		},
		// 更换工程经理完成
		changeManSuccessFunc() {
			this.$emit('submit', this.value);
			this.close('edit_manager');
		},
		close(type) {
			switch (type) {
				case 'add_manager':
					this.gcjlShow = false;
					break;
				case 'refuse_orders':
					this.gcjljjjdVisible = false;
					break;
				// 更换工程经理
				case 'edit_manager':
					this.changShow = false;
					break;
				// 质量评分
				case 'projects_projectQualityRating':
					this.zlpfShow = false;
					break;
				default:
			}
			this.$emit('update:value', '');
			this.fpgcjlRuleForm = {
				engineering_manager_id: '',
				engineering_manager_name: '',
				engineering_supervision_id: '',
				engineering_supervision_name: '',
				remark: ''
			};
		},
		// 工程经理列表
		getEngineering_directorList(type) {
			var obj = {
				page: 1,
				num: 9999,
				keyword: '',
				province_id: '',
				city_id: '',
				county_id: '',
				identity: type == 7 ? 2 : 1,
				position_id_arr: [type]
			};
			this.$Apipc
				.getAdminList(obj)
				.then((res) => {
					console.log(res, type == 8 ? '工程监理8' : '工程经理7');
					if (res.code == 200) {
						if (type == 7) {
							this.engineeringSupervisionList = res.data.list;
						} else {
							this.engineeringSupervisionIdList = res.data.list;
						}
					}
				})
				.catch((err) => {});
		},
		// 选择开启
		choosefpgcjFunc(type) {
			if (type == 8 && !this.fpgcjlRuleForm.engineering_manager_name) {
				uni.showToast({
					title: '请先选择工程经理',
					icon: 'error'
				});
				return;
			}
			this.type = type;
			this.columnsList = type == 7 ? [this.engineeringSupervisionList] : [this.engineeringSupervisionIdList];
			let index = 0;
			if (type == 8) {
				index = this.engineeringSupervisionIdList.findIndex((item) => {
					return item.id == this.fpgcjlRuleForm.engineering_supervision_id;
				});
			} else {
				index = this.engineeringSupervisionList.findIndex((item) => {
					return item.id == this.fpgcjlRuleForm.engineering_manager_id;
				});
			}
			this.gcjlShow = false;
			this.gcjlShow2 = true;
			this.$nextTick(() => {
				this.$refs.picker.setIndexs([index == -1 ? 0 : index], true);
			});
		},
		// 选择关闭
		choosefpgcjFunc2() {
			this.gcjlShow = true;
			this.gcjlShow2 = false;
		},
		// 确认选择
		gcjlConfirm(e) {
			console.log(e);
			if (this.type == 7) {
				this.fpgcjlRuleForm.engineering_manager_id = e.value[0].id;
				this.fpgcjlRuleForm.engineering_manager_name = e.value[0].name;
				let obj = this.engineeringSupervisionList.find((item) => {
					return item.id == e.value[0].id;
				});
				if (obj) {
					this.fpgcjlRuleForm.engineering_supervision_id = obj.engineering_supervision_id;
					this.fpgcjlRuleForm.engineering_supervision_name = obj.engineering_supervision_name;
				}
			} else {
				this.fpgcjlRuleForm.engineering_supervision_id = e.value[0].id;
				this.fpgcjlRuleForm.engineering_supervision_name = e.value[0].name;
			}
			this.choosefpgcjFunc2();
		},
		submitFunc() {
			let fpgcjlRuleForm = this.fpgcjlRuleForm;
			console.log(fpgcjlRuleForm);
			if (!fpgcjlRuleForm.engineering_manager_name) {
				uni.showToast({
					title: '请选择工程经理',
					icon: 'error'
				});
				return;
			}
			if (!fpgcjlRuleForm.engineering_supervision_name) {
				uni.showToast({
					title: '请选择工程监理',
					icon: 'error'
				});
				return;
			}
			this.assignEngineeringManager(fpgcjlRuleForm);
		},
		assignEngineeringManager(data) {
			uni.showLoading({
				mask: true
			});
			data.project_id = this.oper_item.id;
			data.type = 2;
			this.$Apipc.assignEngineeringManager(data).then((res) => {
				console.log(res, '分配施工工程经理');
				uni.hideLoading();
				if (res.code == 200) {
					this.$emit('submit', this.value);
					this.close('add_manager');
				}
				uni.showToast({
					icon: res.code == 200 ? 'success' : 'error',
					title: res.code == 200 ? '处理成功' : res.message
				});
			});
		},
		// 拒绝接单
		jjSubmitFunc() {
			let gcjljjjdRuleForm = this.gcjljjjdRuleForm;
			console.log(gcjljjjdRuleForm);
			if (!gcjljjjdRuleForm.remark) {
				uni.showToast({
					title: '请输入拒绝理由',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				mask: true
			});
			gcjljjjdRuleForm.project_id = this.oper_item.id;
			this.$Api.refuseAssignment(gcjljjjdRuleForm).then((res) => {
				console.log(res, '拒接接单');
				uni.hideLoading();
				if (res.code == 200) {
					this.$emit('submit', this.value);
					this.close('refuse_orders');
				}
				uni.showToast({
					icon: res.code == 200 ? 'success' : 'error',
					title: res.code == 200 ? '处理成功' : res.message
				});
			});
		},
		// 质量评分
		zlpfSubmitFunc() {
			let zlpfRuleForm = this.zlpfRuleForm;
			console.log(zlpfRuleForm);
			if (!zlpfRuleForm.project_quality_rating) {
				uni.showToast({
					title: '请先打分',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				mask: true
			});
			this.$Apipc
				.projectQualityRating({
					project_quality_rating: zlpfRuleForm.project_quality_rating,
					project_id: this.oper_item.id
				})
				.then((res) => {
					console.log(res, '质量评分');
					uni.hideLoading();
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('projects_projectQualityRating');
					}
					uni.showToast({
						title: res.message,
						icon: res.code == 200 ? 'success' : 'error'
					});
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.fp-box-title {
	height: 46px;
	border-bottom: 2rpx solid #efefef;
	text-align: center;
	line-height: 46px;
	font-size: 30rpx;
}
.fp-box {
	width: 100%;
	padding: 20rpx 30rpx 20rpx;
	box-sizing: border-box;
	.u-rate {
		margin: 10rpx;
	}
}
</style>
